package com.naire.modul.cusorg.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.common.dao.GeneralDaoImpl;
import net.common.service.PageBean;
import net.common.util.BeanUtils;

import com.naire.modul.cusorg.dao.CusorgDao;
import com.naire.modul.cusorg.pojo.Cusorg;
import com.naire.modul.cusorg.vo.CusorgVo;
import com.naire.modul.district.pojo.District;

public class CusorgDaoImpl extends GeneralDaoImpl implements CusorgDao {

	@SuppressWarnings("unchecked")
	public List<CusorgVo> searchByNum(PageBean pageBean, String dict_num, String parent_code)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		StringBuilder hql = new StringBuilder("from Cusorg where flag=0");
		if (dict_num != null && !dict_num.equals("")) {
			hql.append(" and industry=:industry");
			map.put("industry", dict_num);
		}
		if (parent_code != null && !parent_code.equals("")) {
			hql.append(" and parent_num=:parent_num");
			map.put("parent_num", parent_code);
		}
		List<CusorgVo> d = new ArrayList<CusorgVo>();
		List<Cusorg> l = hibernateHandler.query(hql,pageBean, map);
		for (Cusorg bean : l) {
			CusorgVo dv = new CusorgVo();
			BeanUtils.copyProperties(bean, dv);
			District c = bean.getDistrict();
			if (c != null) {
				dv.setDistrict_name(c.getDistrict_name());
			}
			d.add(dv);
		}
		return d;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CusorgVo> search(PageBean pageBean, Integer cust_id)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("cust_id", cust_id);
		StringBuilder hql = new StringBuilder(
				"from Cusorg where flag=0 and customer.cust_id=:cust_id");
		List<CusorgVo> d = new ArrayList<CusorgVo>();
		List<Cusorg> l = hibernateHandler.query(hql, pageBean, map);
		for (Cusorg bean : l) {
			CusorgVo dv = new CusorgVo();
			BeanUtils.copyProperties(bean, dv);
			District c = bean.getDistrict();
			if (c != null) {
				dv.setDistrict_name(c.getDistrict_name());
			}
			d.add(dv);
		}
		return d;
	}

	@Override
	public void insert(CusorgVo bean) throws Exception {
		Cusorg pojo = new Cusorg();
		BeanUtils.copyProperties(bean, pojo);
		if (bean.getDistrict_id() != null) {
			District c = (District) hibernateHandler.find(District.class,
					bean.getDistrict_id());
			pojo.setDistrict(c);
		}
		hibernateHandler.insert(pojo);
	}

	@Override
	public void update(CusorgVo bean) throws Exception {
		Cusorg pojo = (Cusorg) hibernateHandler.find(Cusorg.class,
				bean.getOrg_id());
		BeanUtils.copyProperties(bean, pojo);
		if (bean.getDistrict_id() != null) {
			District c = (District) hibernateHandler.find(District.class,
					bean.getDistrict_id());
			pojo.setDistrict(c);
		}
		hibernateHandler.update(pojo);
	}

	@Override
	public void updateFlag(CusorgVo bean) throws Exception {
		update(bean);
	}

	@Override
	public CusorgVo find(Integer id) throws Exception {
		Cusorg pojo = (Cusorg) hibernateHandler.find(Cusorg.class, id);
		CusorgVo bean = new CusorgVo();
		BeanUtils.copyProperties(pojo, bean);
		District district = pojo.getDistrict();
		if (district != null) {
			bean.setDistrict_id(district.getDistrict_id());
			bean.setDistrict_name(district.getDistrict_name());
		}
		return bean;
	}
}
